package com.cskaoyan.gateway.controller.shopping;

import com.cskaoyan.gateway.form.shopping.GoodsForm;
import com.cskaoyan.gateway.form.shopping.SearchData;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Walkden Piao
 * @version 1.0
 */

/**
 * @Api
 * value - 字段说明
 * description - 注释说明这个类
 */
@Slf4j //这是生成日志文件的注解
@RestController
@RequestMapping("/shopping")
@Api(tags = "ProductController", description = "商品展示层")
public class ProductController {

    @Reference(timeout = 3000,check = false, retries = 0)
    IProductService iProductService;
    /**
     * 根据商品ID获取商品详情页
     */
    @GetMapping("/product/{id}")
    @ApiOperation("获取当前商品信息的详情页")
    @Anoymous
    public ResponseData productDetail(@PathVariable("id") Long id){
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(id);
        ProductDetailResponse response = iProductService.getProductDetail(productDetailRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 分页查询商品列表
     * @param form 参数封装
     * @return ResponseData
     */
    @Anoymous
    @GetMapping("/goods")
    public ResponseData<?> getGoods(GoodsForm form){
        AllProductRequest allProductRequest = new AllProductRequest();
        allProductRequest.setPage(form.getPage());
        allProductRequest.setSize(form.getSize());
        allProductRequest.setSort(form.getSort());
        allProductRequest.setCid(form.getCid());
        allProductRequest.setPriceGt(form.getPriceGt());
        allProductRequest.setPriceLte(form.getPriceLte());
        AllProductResponse response = iProductService.getAllProduct(allProductRequest);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            Map<String, Object> map = new HashMap<String, Object>(){{
                put("data", response.getProductDtoList());
                put("total", response.getTotal());
            }};
            return new ResponseUtil<>().setData(map);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 展示推荐商品
     */
    @GetMapping("/recommend")
    @ApiOperation("获取推荐商品信息")
    @Anoymous
    public ResponseData<?> recommendProduct(){
        RecommendResponse response = iProductService.getRecommendGoods();

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode()))
            return new ResponseUtil<>().setData(response.getPanelContentItemDtos());
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
